import {BaseButton, BaseInput, BaseModel, BasePage, BaseRule} from "../../../ts/type";
import {confirm, replace, util} from "../../../ts/util";
import post from "../../../axios";

export let extraConfig = (page: BasePage) => {
    replace(page, {
        columns: [
            {},
            {},
            {exist: false},
            {exist: false},
            {exist: false},
            {exist: false},
            {exist: false},
            {exist: false},
            {},
            {exist: false},
            {exist: false},
            {exist: false},
            {exist: false},
            {exist: false},
            {exist: false},
            {exist: false},
            {exist: false},
            {exist: false},
            {exist: false},
            {exist: false},
            {exist: false},
            {exist: false},
            {exist: false},
            {},
            {},
            {exist: false},
            {exist: false},
            {exist: false},
            {exist: false}
        ],
        models: [
            {
                formInputs: [
                    {},
                    {},
                    {},
                    {},
                    {},
                    {},
                    {},
                    {},
                    {},
                    {},
                    {},
                    {},
                    {exist: false},
                    {exist: false},
                    {exist: false},
                    {exist: false},
                    {exist: false},
                    {exist: false},
                    {exist: false},
                    {exist: false},
                    {exist: false},
                    {exist: false}
                ]
            },
            {},
            {},
            {},
            replace(new BaseModel('bindCode', '绑定编码模板', [
                replace(new BaseInput('字段编码', 'fieldCode'), {
                    hidden: true
                }),
                replace(new BaseInput('模板名称', 'codeName'), {
                    inputType: 'select',
                    optionsUrl: '/au/codeTemplate/codeName/select'
                })
            ], [
                replace(new BaseButton('绑定', () => {
                    page.currentModel.formRef?.validate((errors: any) => {
                        if (errors) {
                            util.msg?.error('请检查填写内容')
                        } else {
                            // 是否已绑定过
                            post('/au/columnCode/selectByFieldCode', page.currentModel.model).then((res) => {
                                if (res) {
                                    // 有绑定弹出覆盖确认
                                    confirm('该字段已绑定编码模板，是否覆盖？', () => {
                                        post('/au/columnCode/updateByFieldCode', page.currentModel.model).then(() => {
                                            util.msg?.success('绑定成功')
                                            page.currentModel.exist = false
                                        })
                                    })
                                } else {
                                    // 无绑定 直接绑定
                                    post('/au/columnCode/save', page.currentModel.model).then(() => {
                                        util.msg?.success('绑定成功')
                                        page.currentModel.exist = false
                                    })
                                }
                            })
                        }
                    })
                }), {
                    type: 'info'
                }),
                new BaseButton('取消', () => {
                    page.currentModel.exist = false
                })
            ], page), {
                rules: {
                    codeName: new BaseRule()
                }
            })
        ],
        operationColumnExtOperation: [
            replace(new BaseButton('复制', (row: any) => {
                page.currentModel = page.models.filter(m => 'save' == m.formKey)[0]
                page.currentModel.formReset()
                page.currentModel.exist = true
                post(page.url.selectById, {id: row.id}).then((res: any) => {
                    page.currentModel.model = res
                    page.currentModel.model.fieldCode = null
                })
            }), {
                type: 'info'
            }),
            replace(new BaseButton('绑定编码模板', (row: any) => {
                page.currentModel = page.models.filter(m => 'bindCode' == m.formKey)[0]
                page.currentModel.formReset()
                page.currentModel.model.fieldCode = row.fieldCode
                page.currentModel.exist = true
            }), {
                type: 'info'
            })
        ]
    })
}